from django.contrib.auth.models import User
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from knowledge.models import Article, Child, Classify
from django.views.decorators.csrf import csrf_exempt


# Create your views here.


def article_all(article_model_queryset):
    article_all_list = []
    for x in article_model_queryset:
        id1 = x.id
        title = x.title
        author = x.author.username
        publish_time = str(x.publish_time)[:16]
        content = x.content
        classify = str(x.classify)
        # uploadfile = x.uploadfile
        article_all_list.append({"id": id1, "title": title, "author": author, "publish_time": publish_time,
                                 "classify": classify, "content": content})
        print('22')
        print(article_all_list)
    return article_all_list
    # 这里翻了个低级错误，把return放在了循环里面，放里面了就表示当前循环只循环了一次，所以之前一直只有一个数据


def article_form(request):
    article_all_obj = Article.objects.all()
    print("我在这里29")
    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": article_all(article_all_obj)})


@csrf_exempt
def create_article(request):
    field_back_title = request.POST.get('field_back[title]')
    field_back_author = request.POST.get('field_back[author]')
    field_back_publish_time = request.POST.get('publish_time_back')
    field_back_publish_time_str = str(field_back_publish_time)
    field_back_content = request.POST.get('content_back')
    print(field_back_content)
    field_back_content_str = str(field_back_content)
    field_back_classify = request.POST.get('classify_back')
    print('wqeq')
    print(field_back_classify)
    print('dqwrweew')
    classify_tmp = Child.objects.get(child_list=field_back_classify)
    # 这个分类注意，是个外键，如果直接取会报错：必须是一个实例
    author_tmp = User.objects.get(username=field_back_author)
    # 从User表中找到传入作者名字的那条记录，在数据库中存的是author_id
    print('11111')
    create_article_obj = Article.objects.create(title=field_back_title,
                                                author=author_tmp,
                                                publish_time=field_back_publish_time_str,
                                                content=field_back_content_str,
                                                classify=classify_tmp)
    print("我在这里50")
    print(create_article_obj)
    article_all_obj = Article.objects.all()
    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": article_all(article_all_obj)})


@csrf_exempt
def input_title(request):
    search_input_title = request.POST.get('search_input_title')
    search_input_title_obj = Article.objects.filter(title__icontains=search_input_title)  # 过滤出包含前端输入的标题
    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": article_all(search_input_title_obj)})


@csrf_exempt
def input_author(request):
    search_input_author = request.POST.get('search_input_author')
    print(search_input_author)
    search_input_author_obj = Article.objects.filter(author__username__icontains=search_input_author)
    print(search_input_author_obj)
    print('81')
    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": article_all(search_input_author_obj)})


def acquire_list(request):
    select_option_all = Child.objects.all()  # 获取所有对象，这个操作出来的结果并不是JSON格式，要转换
    print('98')
    option_choice = []
    for x in select_option_all:
        option_choice.append(x.child_list)
    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": option_choice})


@csrf_exempt
def detail_article(request):  # 查
    select_option_all = Child.objects.all()  # 获取所有对象，这个操作出来的结果并不是JSON格式，要转换
    print('98')
    option_choice = []
    for x in select_option_all:
        option_choice.append([x.id, x.child_list])
    print(option_choice)
    article_this_id = request.POST.get('id_back')
    article_select_this = Article.objects.get(id=article_this_id)  # get出来一个对象
    a = str(article_select_this.publish_time)
    b = (a[:16])

    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": [article_select_this.title,
                                  article_select_this.author.username,
                                  article_select_this.publish_time,
                                  b,
                                  article_select_this.classify.child_list,
                                  article_select_this.content,
                                  [option_choice]]})


@csrf_exempt
def edit_article(request):
    article_field_id_obj = request.POST.get('article_id')
    article_field_title_obj = request.POST.get('field_title_back')
    article_field_author_obj = request.POST.get('field_author_back')
    author_tmp = User.objects.get(username=article_field_author_obj)
    article_field_publish_time_obj = request.POST.get('publish_time_back')
    field_back_publish_time_str = str(article_field_publish_time_obj)
    article_field_classify_obj = request.POST.get('classify_back')
    article_field_classify_obj_str = str(article_field_classify_obj)
    print('egirgherignerbai')
    print(article_field_classify_obj_str + '     111')
    classify_tmp = Child.objects.get(child_list=article_field_classify_obj_str)
    article_field_content_obj = request.POST.get('content_back')
    article_edit_obj = Article.objects.get(id=article_field_id_obj)
    article_edit_obj.title = article_field_title_obj
    article_edit_obj.author = author_tmp
    article_edit_obj.publish_time = field_back_publish_time_str
    article_edit_obj.classify = classify_tmp
    article_edit_obj.content = article_field_content_obj
    article_edit_obj.save()
    print('asdasdasdasdasda')
    print('143')
    print(article_edit_obj)

    return JsonResponse({"code": 0,
                         "msg": "",
                         "count": 1000,
                         "data": [article_edit_obj.title,
                                  article_edit_obj.author.username,
                                  article_edit_obj.publish_time,
                                  article_edit_obj.classify.child_list,
                                  article_edit_obj.content]})


def last_few_list(request):
    pass
